/**
 * Created by Administrator on 2017/10/5 0005.
 */
(function ($j) {
    function init() {
        renderData();
        bindBtnEvent();
    }

    function renderData() {
        var _val = $('#search').val();
        var param = {
            name: _val,
            phone: _val
        }
        $.showPreloader();
        $j.restPost('/order/doQueryAdmin', param).done(function (mess, data) {
            renderTable(data.list);
            $.hidePreloader();
        }).fail(function (mess) {
            $.toast(mess);
            $.hidePreloader();
        });

        //查询推荐人信息列表
        $j.restPost('/referrer/doQueryAdmin').done(function(mess,data){
            var content = '';
            $(data.list).each(function(i,item){
                content += '<tr data-id="' + item.orderId + '"><td>' + item.name + '</td><td>' + item.phone + '</td><td>' + item.status + '</td><td>' + item.referName + '</td><td>' + item.rebate + '</td><td>' + $j.getTargetFullDate(item.createTime) + '</td><td>' + createDeleteBtn(item.orderId) +'</td></tr>';
            });
            $('#referrer').html(content);
        }).fail(function(mess){
            $.toast(mess);
        })

        function renderTable(list){
            var install = '';
            var renew = '';
            var repair = '';
            var buyOut = '';
            var rent = '';
            var toRent = '';
            $(list).each(function (i, item) {
                if (item.params == "安装") {
                    install += '<tr data-id="' + item.orderId + '"><td>' + item.name + '</td><td>' + item.phone + '</td><td>' + item.type + '</td><td>' + $j.getTargetFullDate(item.createTime) + '</td><td>' + createDeleteBtn(item.orderId) +'</td></tr>';
                }else if (item.params == "付款") {
                    renew += '<tr data-id="' + item.orderId + '"><td>' + item.name + '</td><td>' + item.phone + '</td><td>' + item.type + '</td><td>' + $j.getTargetFullDate(item.createTime) + '</td><td>' + createDeleteBtn(item.orderId) +'</td></tr>';
                }else if (item.params == "报修") {
                    repair += '<tr data-id="' + item.orderId + '"><td>' + item.name + '</td><td>' + item.phone + '</td><td>' + item.type + '</td><td>' + $j.getTargetFullDate(item.createTime) + '</td><td>' + createDeleteBtn(item.orderId) +'</td></tr>';
                }else if (item.params == "买断") {
                    buyOut += '<tr data-id="' + item.orderId + '"><td>' + item.name + '</td><td>' + item.phone + '</td><td>' + item.type + '</td><td>' + $j.getTargetFullDate(item.createTime) + '</td><td>' + createDeleteBtn(item.orderId) +'</td></tr>';
                }else if (item.params == "退租") {
                    toRent += '<tr data-id="' + item.orderId + '"><td>' + item.name + '</td><td>' + item.phone + '</td><td>' + item.type + '</td><td>' + $j.getTargetFullDate(item.createTime) + '</td><td>' + createDeleteBtn(item.orderId) +'</td></tr>';
                }else if (item.params == "改租") {
                    rent += '<tr data-id="' + item.orderId + '"><td>' + item.name + '</td><td>' + item.phone + '</td><td>' + item.type + '</td><td>' + $j.getTargetFullDate(item.createTime) + '</td><td>' + createDeleteBtn(item.orderId) +'</td></tr>';
                }
            });
            $('#install').html(install);
            $('#renew').html(renew);
            $('#repair').html(repair);
            $('#buyOut').html(buyOut);
            $('#toRent').html(toRent);
            $('#rent').html(rent);

            $('.table tbody tr').on('click',function(){
                window.location.href="/user/manage_info?orderId=" + $(this).data('id');
            });

            $('.table tbody tr td .delete').on('click',function(event){
                var $this = $(this);
                $.confirm('确认删除这条记录吗?', function () {
                    $.showPreloader();
                    $j.restPost('/order/doRemove/' + $(this).data('id')).done(function(mess,data){
                        $this.parents('tr').remove();
                        $.hidePreloader();
                    }).fail(function(mess){
                        $.toast(mess);
                        $.hidePreloader();
                    })
                });
                event.stopPropagation();
            });


            
        }
    }

    var createDeleteBtn = function (orderId) {
        return '<a href="#" data-id="' + orderId + '" class="button button-fill button-danger delete">删除</a>';
    }


    function bindBtnEvent() {
        //输入框事件
        $('.doSearch').off('click').on('click', function () {
            $.showPreloader();
            renderData();
        });

    }

    init();
})(jQuery);